import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/style.dart';
import 'package:group_button/group_button.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/life/LjgmOrder.dart';

import 'Login.dart';

/**
 * 服务预约页面
 */
class MyYuYue extends StatefulWidget {
  // final String gid; //商品ID
  MyYuYue({Key key}) : super(key: key);

  @override
  _MyYuYue createState() => _MyYuYue();
}

class _MyYuYue extends State<MyYuYue> {
  // 构造方法
  _MyYuYue() {
    _onLoading();
  }

  bool isClick = true;    //是否点击
  List yuyueList = [];    //预约集合
  int page = 1;           //页数
  String userId;          //用户ID

  // final BoxDecoration activeBG = BoxDecoration(     // 点击的背景色
  //     color: Colors.lightGreen
  // );
  // final BoxDecoration unactiveBG = BoxDecoration(   // 未点击的背景色
  //   color: Colors.lime
  // );

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }
  // 上拉加载控制器
  RefreshController _refreshController = RefreshController(initialRefresh: false);
  void _onRefresh() async{
    // monitor network fetch
    await Future.delayed(Duration(milliseconds: 1000));
    // if failed,use refreshFailed()
    _refreshController.refreshCompleted();
  }

  // 加载数据的函数
  void _onLoading() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    userId = prefs.getString("userId");
    var x = await HttpUtil().get("/waimai/merchantYuyue/getMerchantYuyue?limit=10&page=${page}&userId="+userId+"&type=0"+"&status=10");
    page++;
    if(x['code']==200){
      yuyueList.addAll(x['data']);
    }
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 464, height: 820);
    return new Scaffold(
      appBar: AppBar(
        title: Text("我的预约"),
      ),
      body: Column(
        children: [
          // Row(
          //   mainAxisAlignment: MainAxisAlignment.center,
          //   children: [
          //     GestureDetector(
          //       child: Container(
          //         padding: EdgeInsets.all(10),
          //         child: Text("未完成预约", style: TextStyle(color: Colors.white),),
          //         decoration: isClick==true? activeBG:unactiveBG
          //       ),
          //       onTap: (){
          //         setState(() {
          //           if(isClick==false)
          //             isClick=!isClick;
          //         });
          //       },
          //     ),
          //     GestureDetector(
          //       child: Container(
          //         padding: EdgeInsets.all(10),
          //         child: Text("已完成预约", style: TextStyle(color: Colors.white),),
          //         decoration:isClick==true? unactiveBG :activeBG
          //       ),
          //       onTap: (){
          //         setState(() {
          //           if(isClick==true)
          //           {
          //             isClick=!isClick;
          //             //这里请求完成预约的数据
          //             // goods.add( {"dianpu":"一帘幽梦","shijian":"2021-9-22","xiangmu":"桑拿33"},);
          //           }
          //         });
          //       },
          //     )
          //   ],
          // ),
          Expanded(child: SmartRefresher(
            enablePullDown: false,
            enablePullUp: true,
            header: WaterDropHeader(),
            footer: CustomFooter(
              builder: (BuildContext context,LoadStatus mode){
                Widget body ;
                if(mode==LoadStatus.idle){
                  body =  Text("上拉加载");
                }
                else if(mode==LoadStatus.loading){
                  body =  CupertinoActivityIndicator();
                }
                else if(mode == LoadStatus.failed){
                  body = Text("加载失败！点击重试！");
                }
                else if(mode == LoadStatus.canLoading){
                  body = Text("松手,加载更多!");
                }
                else{
                  body = Text("没有更多数据了!");
                }
                return Container(
                  height: 55.0,
                  child: Center(child:body),
                );
              },
            ),
            controller: _refreshController,
            onRefresh: _onRefresh,
            onLoading: _onLoading,
            child: ListView.builder(
              itemBuilder: (c, i) => Card(child: _showGoods(yuyueList[i])),
              itemExtent: 100.0,
              itemCount: yuyueList.length,
            ),
          ),)
        ],
      )
    );
  }

  GestureDetector _showGoods(item){
    return GestureDetector(
      child: Container(
        child: Column(
          children: [
            Row(
             mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Text(item['merchant']['mname'],maxLines: 1,style: TextStyle(fontWeight: FontWeight.bold,fontSize: 22,color: Colors.red),),
                ),
                item['status']==0 ? Text("预约中",style: TextStyle(fontSize: 16,color: Colors.amberAccent),) : (item['status']==1 ? Text("已取消预约",style: TextStyle(fontSize: 16,color: Colors.red)) : (item['status']==2 ? Text("预约成功",style: TextStyle(fontSize: 16,color: Colors.green),) : Text("预约超时",style: TextStyle(fontSize: 16,color: Colors.red),)))
              ],
            ),
          SizedBox(height: 10,),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(child: Text(item['mgoods']['gname'],maxLines: 1,),)
              // Text(item['createTime'],style: TextStyle(color: Colors.black),)
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text("预约人数："+item['num'].toString()),
              Text("预约时间："+item['yuyueTime'].toString()),
            ],
          )],
        ),
      ),
      onTap: (){

      },
    );
  }

}
